package ysf.com.simplediffculty;

/**
 * 二十八
 * 给定一个字符传 和目标字符串进行比较如果存在就返回字符的长度，不存在就返回-1
 * @author ysf
 * @date 2021/8/17 13:57
 */
public class TwentyEight {

    public static void main(String[] args) {

        String haystack = "hello";
        String needle = "lll";
        /*int i = compareCharacter(haystack, needle);
        System.out.println(i);*/
        int i = compareChacter(haystack, needle);
        System.out.println(i);

    }


    public static int compareCharacter(String haystack,String needle){
        if(needle.length() == 0){
            return 0;
        }
        /*if(needle.length() > haystack.length()){
            return -1;
        }*/
        int j = 0;
        for (int i = 0; i < haystack.length(); i++) {
            if(haystack.charAt(i) == needle.charAt(j) ){
                j++;
                if(needle.length() == j){
                    return j;
                }
            }else {
                i = i -j;
                j = 0;
            }
        }
        return -1;
    }

    public static int compareChacter(String haystack,String needle){
        if(needle.length() == 0){
            return 0;
        }
        int j = 0;
        for (int i = 0; i < haystack.length(); i++) {
            if(haystack.charAt(i) == needle.charAt(j)){
                j++;
                if(needle.length() == j){
                    return j;
                }
            }
        }
        return -1;
    }



}
